//
//  ZJIntroductionView.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/8/23.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJIntroductionView.h"
#import "UIButton+countDown.h"
#import "UIImage+GIF.h"
#import <ImageIO/ImageIO.h>
@interface ZJIntroductionView ()
{
    UIButton *button;
}
@end

@implementation ZJIntroductionView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor whiteColor];
        [self showIntroductionView];
    }
    return self;
}

- (void)showIntroductionView{
  
    //gif view
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.frame];
    NSString* path = [[NSBundle mainBundle] pathForResource:@"huanyingye-gif2" ofType:@"gif"];
    NSData  *imageData = [NSData dataWithContentsOfFile:path];
    imageView.image = [self animatedGIFWithData:imageData];
    //Button
    CGFloat width = [NSString sizeOfString:@"4 跳过引导" font:14 * [UIScreen zj_textFontScale] width:[UIScreen zj_screenWith]].width + 15 * [UIScreen zj_screenWScale];
    CGFloat heigth = [NSString sizeOfString:@"4 跳过引导" font:14 * [UIScreen zj_textFontScale] width:[UIScreen zj_screenWith]].height + 15 * [UIScreen zj_screenWScale];
    
    button = [[UIButton alloc]initWithFrame:CGRectMake([UIScreen zj_screenWith] - width - 10 * [UIScreen zj_screenWScale], 22, width, heigth)];
    button.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [button setTitle:@"4 跳过引导" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(action_romveIntroductionView) forControlEvents:UIControlEventTouchUpInside];
    button.layer.cornerRadius = 4 * [UIScreen zj_screenHScale];
    button.titleLabel.font = [UIFont subTitleTextFont];
    [self addSubview:imageView];
    [self addSubview:button];

}

- (void)showOnViewController:(UIViewController *)viewController{
    [viewController.view addSubview:self];
    [button startWithTime:4 title:@"跳过引导" countDownTitle:@"跳过引导" mainColor:[UIColor whiteColor] countColor:[UIColor whiteColor] isIntroductionView:YES];
    
    [self performSelector:@selector(action_romveIntroductionView) withObject:nil afterDelay:4.0f];
    
}

- (void)action_romveIntroductionView{
    [[NSNotificationCenter defaultCenter] postNotificationName:FINGERPRINT object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(action_romveIntroductionView) object:nil];
    [self removeFromSuperview];
}

//仿造SDWebImage解析GIF
- (UIImage *)animatedGIFWithData:(NSData *)data {
    if (!data) {
        return nil;
    }
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    
    size_t count = CGImageSourceGetCount(source);
    
    UIImage *animatedImage;
    
    if (count <= 1) {
        animatedImage = [[UIImage alloc] initWithData:data];
    }
    else {
        NSMutableArray *images = [NSMutableArray array];
        
        for (size_t i = 0; i < count; i++) {
            CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
            
            UIImage * tempImage = [UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
            if (i % 2 == 0) {
                for ( int j = 0; j < 7; j ++) {
                    [images addObject:tempImage];
                }
            }
            else{
                [images addObject:tempImage];
            }
            
            CGImageRelease(image);
        }
        
        animatedImage = [UIImage animatedImageWithImages:images duration:4];
    }
    
    CFRelease(source);
    
    return animatedImage;
}


@end
